package com.security.symmetry_encry;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi;
import org.bouncycastle.jcajce.provider.symmetric.DES;
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
import java.security.SecureRandom;

/**
 * @ClassName DESTest
 * @Description DES 对称加密
 * @Detail detail
 * @Author MyPC
 * @Date 2020/11/22
 * @Version 1.0
 */
public class DESTest {
    // DES
    public static final String ALGORITHM = "DES";

    public static void main(String[] args) throws Exception {
        String inputStr = "DES";
//        String key = initKey();
        String key = initKey("11");
        System.err.println("原文:\t" + inputStr);

        System.err.println("密钥:\t" + key);

        byte[] inputData = inputStr.getBytes();
        inputData = encrypt(inputData, key);
        System.out.println("加密后："+new BASE64Encoder().encode(inputData));
        byte[] decrypt = decrypt(inputData, key);
        System.out.println("解密后："+new String(decrypt));
    }

    /**
     * 生成密钥
     *
     * @return
     * @throws Exception
     */
    public static String initKey() throws Exception {
        return initKey(null);
    }

    /**
     * 初始化密钥
     *
     * @param seed
     * @return
     * @throws Exception
     */
    public static String initKey(String seed) throws Exception {
        // 安全随机数
        SecureRandom secureRandom = null;

        if (seed != null) {
            secureRandom = new SecureRandom(new BASE64Decoder().decodeBuffer(seed));
        } else {
            secureRandom = new SecureRandom();
        }
        // 密钥生成器
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
        kg.init(secureRandom);

        // 生成密钥
        SecretKey secretKey = kg.generateKey();

        return new BASE64Encoder().encodeBuffer(secretKey.getEncoded());
    }

    /**
     * 转换密钥
     * @param key 初始密钥
     * @return
     * @throws Exception
     */
    private static Key toKey(byte[] key) throws Exception{
        // 将JDK生成的密钥转化成DES规则的密钥对象
        DESKeySpec desKeySpec = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(desKeySpec);
    }

    /**
     * 加密
     * @param data 要加密的数据
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] data, String key) throws Exception {
//        Key k = toKey( new BASE64Decoder().decodeBuffer(key));
        // 密钥使用base64进行编码
        Key k = toKey(Base64.decodeBase64(key));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE,k);
        return cipher.doFinal(data);
    }

    /**
     * 解密
     * @param data  要解密的数据
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, String key) throws Exception {
        // 密钥使用base64进行解码
        Key k = toKey( new BASE64Decoder().decodeBuffer(key));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE,k);
        return cipher.doFinal(data);
    }


}
